{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Robust Mølmer-Sørensen gate in Ion trap\n",
    "\n",
    "*Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.*\n",
    "\n",
    "## Outline\n",
    "\n",
    "In this tutorial, we will introduce how to perform a robust Mølmer-Sørensen gate in Quanlse. The outline of this tutorial is as follows:\n",
    "\n",
    "- Introduction\n",
    "- Fidelity of two qubit Mølmer-Sørensen gate\n",
    "- Noise of two qubit Mølmer-Sørensen gate\n",
    "- Ways to resist noise\n",
    "- Construct pulse sequences of the robust two qubit Mølmer-Sørensen gate\n",
    "- Summary\n",
    "- References"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction\n",
    "\n",
    "Two-qubit gates in trapped ions are built by Mølmer-Sørensen (MS) interactions between different ions. Such interaction couples an ion with the phonon mode $\\omega_k$ of the ion chain through individual Raman beams operated on this ion. If two ions are coupled with the phonon mode $\\omega_k$ of the ion chain respectively, these two ions will connect to each other. Such a process is called Mølmer-Sørensen interactions. The resulting two-qubit gate is called MS gate. As for two-qubit gates between ion $i$ and ion $j$, we wish to preserve the coupling between ion $i$ and ion $j$, represented as $\\phi_{ij}$. At the same time, eliminate the coupling between phonon mode $\\omega_k$ and ions, expressed as $\\alpha_{ki} ,  \\alpha_{kj}$ .\n",
    "\n",
    "The operation in the ion trap is almost the most powerful and most accurate in the mainstream quantum platforms. But there still would be a long way from being engaged. In order to perform large-scale quantum operation experimentally, we need to build a gate pulse sequence that is robust to dephasing noise and timing noise. Affected by Raman frequency draft and the line width of the laser beams, the phonon-ion coupling strength $\\alpha$ cannot return to zero after a gate pulse time. Hence, such noise reduces the fidelity of the two-qubit gate, which is called dephasing noise. Similarly, the scaling variation of the gate pulse time can also reduce the gate fidelity, called timing noise. We focus on how to get a robust Mølmer-Sørensen that can reduce these two kinds of noise."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fidelity of Mølmer-Sørensen gate\n",
    "\n",
    "In our experiment, for ion $j$, we need a red Raman beam and a blue Raman beam to realize MS interactions. In such a protocol, the red beam Raman has optical phase $\\phi^r_j$ and the blue Raman beam has optical phase $\\phi^b_j$. We set $\\phi^S_j=(\\phi^r+\\phi^b_j)/2$ and $\\phi^M_j=(\\phi^r_j-\\phi^b_j)/2$. The MS interaction can be represented by Hamiltonian rotating on $\\phi^S_j$ around Bloch Sphere:\n",
    "\n",
    "$$\n",
    "H_{MS}=\\sum_j \\sum_{k=0}^{N-1}\\hbar\\eta_{k,j}\\Omega_j\\cos(\\mu t-\\phi_j^M)(\\hat{a}_ke^{-i\\omega_k t}+\\hat{a}_k^\\dagger e^{i\\omega_k t})\\hat{\\sigma}_\\phi^{(j)}.\n",
    "$$\n",
    "\n",
    "Where the effective Pauli matrix is $\\hat{\\sigma}_\\phi^{(j)}=\\cos(\\phi^S_j)\\sigma_x^{(j)}+\\sin(\\phi^S_j)\\sigma_y^{(j)}$, and $\\eta_{k,j}$ is the coupling parameter of ion $j$ and phonon mode $k$, which usually called Lamb-Dicke parameter. $\\Omega_j$ is the Rabi frequency of ion $j$ affected by the Raman pulse intensity. $\\mu$ is the detuning of Raman frequency and energy level between $|0\\rangle,|1\\rangle$, $\\hat{a}_k$ is the annihilation operator of phonon mode $k$. Under such Hamiltonian, we can simplify the phonon-ion evolution matrix using Magnus expansion:\n",
    "\n",
    "$$\n",
    "U(\\tau)=\\exp\\left[-\\frac{i}{\\hbar}\\int_0^\\tau dtH_{MS}-\\frac{1}{2\\hbar^2}\\int_0^\\tau dt_2\\int_0^{t_2}dt_1[H_{MS}(t_2),H_{MS}(t_1)]+...\\right].\n",
    "$$\n",
    "\n",
    "Plugging in MS interactions, finally, we get：\n",
    "\n",
    "$$\n",
    "U(\\tau)=\\sum_{j}\\sum_{k=0}^{N-1}(\\alpha_{j,k}(\\tau)\\hat{a}^\\dagger+\\alpha_{j,k}^*(\\tau)\\hat{a})\\hat{\\sigma}_\\phi^{(j)}+i\\sum_{m\\neq n}\\chi_{m,n}(\\tau)\\hat{\\sigma}_\\phi^{(m)}\\hat{\\sigma}_\\phi^{(n)}.\n",
    "$$\n",
    "\n",
    "Here $\\alpha_{j,k}$ represents the coupling strength between ion $j$ and phonon $k$, and $\\chi_{m,n}$ represents the coupling strength between ion $m$ and ion $n$, expression ：\n",
    "\n",
    "$$\n",
    "\\alpha_{j,k}(\\tau)=-\\eta_{k,j}\\int_0^\\tau\\Omega_j(t)\\sin(\\mu t-\\phi_j^M)e^{i\\omega_kt}dt,\n",
    "$$\n",
    "$$\n",
    "\\chi_{m,n}(\\tau)=\\sum_{k=0}^{N-1}\\eta_{k,m}\\eta_{k,n}\\int_0^\\tau dt_2\\int_0^{t_2}dt_1\\left[\\Omega_m(t_2)\\Omega_n(t_1)\\sin\\omega_k(t_2-t_1)\\cos(\\mu t_2-\\phi_m^M)\\cos(\\mu t_1-\\phi_n^M)\\right].\n",
    "$$\n",
    "\n",
    "Our purpose is to build two-qubit gates among ions. If phonon-ion coupling strength $\\alpha$ remains, we get a mixed state after the phonon mode environment traces over. In conclusion, the main reason for the infidelity is that the phonon-ion coupling strength $\\alpha$ cannot return 0 after the gate operation. Hence, almost every work that improves the two-qubit gates fidelity revolves around lowering $\\alpha$. For MS gate operation, if we want to reach a coupling strength to $\\phi$ between two ions, the exact expression of fidelity would be:\n",
    "\n",
    "$$\n",
    "F=\\frac{1}{8}[2+2\\cos(2(\\chi_{m,n}-\\phi))(\\Gamma_m+\\Gamma_n)+(\\Gamma_-+\\Gamma_+)],\n",
    "$$\n",
    "\n",
    "where：\n",
    "\n",
    "$$\n",
    "\\Gamma_{m(n)}=e^{-\\sum_{k=0}^{N-1}(|\\alpha_{m(n)),k}(\\tau)|^2\\beta_k/2},\n",
    "$$\n",
    "\n",
    "$$\n",
    "\\Gamma_{\\pm}=e^{-\\sum_{k=0}^{N-1}(|\\alpha_{m,k}(\\tau)\\pm\\alpha_{n,k}(\\tau)|^2\\beta_k/2)},\n",
    "$$\n",
    "\n",
    "$$\n",
    "\\beta_k=\\coth\\left[\\frac{1}{2}\\ln(1+1/\\bar{n}_k(T))\\right].\n",
    "$$\n",
    "\n",
    "$\\bar{n}_k$ is the average phonon number of phonon mode $\\omega_k$ at temperature $T$.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Noise of Mølmer-Sørensen gate\n",
    "\n",
    "We start with an example to show how dephasing and timing noise affect the phonon-ion coupling strength $\\alpha$. We consider a constant Raman pulse interacts with ion $j$, whose Rabi frequency is $\\Omega$, phase $\\phi$, Raman detuning $\\mu$, and pulse time $\\tau$. For MS interactions, such pulse would couple the ion to the phonon mode. For phonon mode $\\omega_k$, we obtain the phonon-ion coupling strength by the integration over the pulse-ion interaction:\n",
    "\n",
    "$$\n",
    "\\alpha_{j,k}(\\tau)=\\frac{\\eta_{k,j}\\Omega_j}{\\mu^2-\\omega_k^2}[e^{i\\omega_k\\tau}(\\mu\\cos(\\mu\\tau-\\phi_j^M)-i\\omega_k\\sin(\\mu\\tau-\\phi_j^M))-(\\mu\\cos\\phi_j^M+i\\omega_k\\sin\\phi_j^M)].\n",
    "$$\n",
    "\n",
    "Notice that $|\\mu-\\omega_k|\\ll\\mu+\\omega_k$. By taking rotating wave approximation, we find the phonon-ion coupling can be expressed as：\n",
    "\n",
    "$$\n",
    "\\alpha(\\tau)\\approx\\frac{\\eta_{k,j}\\Omega_j}{2}(\\frac{e^{i[(\\omega_k-\\mu)\\tau+\\phi_j^M]}-1}{\\mu-\\omega_k}).\n",
    "$$\n",
    "\n",
    "As we see, when the equation satisfies $(\\omega_k-\\mu)\\tau+\\phi_j^M=2n\\pi$, we have $\\alpha(\\tau)=0$. Such condition results in the coupling strength $\\alpha$ return 0 in time length $\\tau$, and we say that the pulse satisfies the phonon decoupling condition. If an external disturbance perturbs the ion system, such as the dephasing noise results in the variation of $\\mu$, timing noise results in the variation of $\\tau$. $\\alpha$ will not return 0 in time $\\tau$.\n",
    "\n",
    "![noise_alpha](figures/tutorial_noise.png)\n",
    "\n",
    "We set the laser parameters in the left picture that satisfies the decoupling condition. Where $\\mu$=1.6MHz, $\\tau$=63 $\\mu s$. But in the right picture, we set the dephasing noise that changes $\\mu$=1.48MHz and timing noise changes $\\tau$=53.6 $\\mu s$ with the drift of 16KHz and 15%. And that resulted in a significant drop in infidelity. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ways to resist noise\n",
    "\n",
    "Above, we have seen that a constant pulse is not robust for dephasing, timing noise that the phonon-ion coupling strength cannot return 0 after the pulse time $\\tau$. We can mitigate this problem by using symmetry manipulations over the pulse sequence. For example, we can design the laser pulse to be asymmetrical, using the symmetry property to eliminate the global drift act on $\\alpha$.\n",
    "\n",
    "As an instance for a two-ion trap system, we can divide our laser pulse into 15 pieces and reshape the last 7 pulse pieces to become amplitude symmetrical and phase antisymmetric to the first 7 pulse pieces, as shown in the figure:\n",
    "\n",
    "![Sympulse](figures/pulse_show.png)\n",
    "\n",
    "We implement such laser pulse sequence on a two-ion trap and calculate the coupling strength $\\alpha_{j,k}$ between two ions and three phonon modes:\n",
    "\n",
    "![alphaSym](figures/alpha_show.png)\n",
    "\n",
    "As we can see, after our symmetrization operations, the phonon-ion coupling strength $\\alpha$ becomes symmetric and circuitous. Such an $\\alpha$ behaves robustly under the dephasing and timing noise. We implemented our robust method in this tutorial around this core idea. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Construct pulse sequences of the Robust Mølmer-Sørensen gate\n",
    "\n",
    "Here we demonstrate an example of generating an robust double MS gate pulse using the symmetry optimization method. Before we start, we should import the corresponding module:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import Robust Mølmer-Sørensen pulse module\n",
    "from Quanlse.TrappedIon.QIonSystem import QChain1D, QLaser\n",
    "from Quanlse.TrappedIon.Optimizer.OptimizerIon import optimizeIonSymmetry\n",
    "from Quanlse.TrappedIon.QIonTrajectory import noiseFeature\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The first step is to set the parameter of the ion trap chip environment and the laser pulse features. As for an ion chip, we set parameters including the ion number `ionNumber`, the ion tag count `indexIon` we have chosen to manipulate, ion mass `mass`, the axial harmonic potential `omegaZ`, transverse harmonic potential `omegaXY` and the ion chain temperature `temperature`. And for control laser, we set parameters like the wavelength `wavelength`, Raman detuning `detuning`, angle between two lasers `laserAngle`, gate pulse sequence time `tau`, pulse segments `segments` and the maximum Rabi rate `omegaMax` (parameters we use in this example are referenced from \\[1\\]).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set experiment parameters\n",
    "# ion trap\n",
    "ionNumber = 10\n",
    "indexIon = [0, 1]           # ion that interact with the laser pulse\n",
    "mass = 171                  # atom mass\n",
    "omegaXY = 22.5e6            # unit: Hz\n",
    "omegaZ = 3.8e6              # unit: Hz\n",
    "temperature = 1e-6          # unit: K\n",
    "# laser\n",
    "waveLength = 369.75         # unit: nm\n",
    "detuning = 3.804e6          # unit: Hz\n",
    "laserAngle = np.pi / 2      # angle between two laser beams\n",
    "tau = 2e-4                  # unit: s\n",
    "segments = 15               # laser pulse segments\n",
    "omegaMax = 62.8e6           # unit: Hz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then, we build up ion chip class `ionChain` and Raman pulse class `ionLaser` from Quanlse:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# generate the entity of ion chip and laser\n",
    "ionChain = QChain1D(ionMass=mass, ionNumber=ionNumber, trapZ=omegaZ, trapXY=omegaXY, temperature=temperature)\n",
    "ionLaser = QLaser(waveLength=waveLength, laserAngle=laserAngle, segments=segments, detuning=detuning, maxRabi=omegaMax,\n",
    "                  tg=tau)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set the expected noise range `noise`. Plug `ionChain`, `indexIon`, `noise` into the optimization function using the symmetry method, and get the optimized pulse sequence `laserFinal`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# use the symmetry method to optimize the laser pulse sequence to be dephasing robust\n",
    "dephasingNoise = 2e3\n",
    "laserFinal = optimizeIonSymmetry(ionChip=ionChain, laser=ionLaser, indexIon=indexIon, noise=dephasingNoise)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At last, we will check out the robustness of our Raman pulse. We generate the ion-ion coupling strength dynamics, infidelity effected by two kinds of noises and the waveform of the Raman pulse. Set the timing noise range `timingNoise` and plug these parameters into the plot function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# show noise features using plot function\n",
    "from Quanlse.TrappedIon.QIonTrajectory import noiseFeature, allAlphaComb\n",
    "timingNoise = 0.001\n",
    "noiseFeature(ionChip=ionChain, laser=laserFinal, indexIon=indexIon, noise=dephasingNoise, timeNoise=timingNoise)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The figure in the upper left represents the ion-ion coupling strength changing with evolution time, figure in the left bottom represents the MS gate infidelity change with dephasing noise. Four figures in the middle represent the laser pulse sequences implemented on two ions. The figure in the upper right represents the ion-ion coupling strength change with dephasing noise, and the figure in the right bottom represents the MS gate infidelity change with timing noise. We can see that the optimized pulse sequence is Rabi frequency symmetrical and phase antisymmetrical, where all Rabi frequency is bounded in the max Rabi frequency to ensure the feasibility of the experiment. Under such a pulse sequence, the ion-ion coupling strength $\\chi$ rises to $\\pi/4$ gradually. And under the dephasing noise and timing noise, the quantum gate has remained low infidelity.\n",
    "\n",
    "As for the phonon-ion coupling strength $\\alpha$, we can use function `allAlphaComb` to plot them in a single canvas:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# show all alpha trajectory\n",
    "allAlphaComb(ionChip=ionChain, laser=laserFinal, index=indexIon)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Clearly, after the interaction with the gate pulse sequence, all phonon-ion coupling strengths return 0, where the blue trajectory correspondent phonon mode closest to the Raman detuning is most effective to the infidelity. And under the interaction of such symmetry pulse sequence, the trajectory return 0 after a gate pulse time, which is an excellent example of how our optimization approach works."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "We can calculate the parameters of the ion trap easily via using Quanlse. And we can generate robust gate pulse sequences under the parameters we set. The visualization module we developed can easily show the variation of phonon-ion coupling strength $\\alpha$ and ion-ion coupling strength $\\chi$. Users can follow this link [tutorial-trapped-ion-robust-control.ipynb](https://github.com/baidu/Quanlse/blob/main/Tutorial/EN/tutorial-trapped-ion-robust-control.ipynb) to the corresponding GitHub page of this Jupyter Notebook to download the code above.\n",
    "\n",
    "We encourage users to try different parameters and explore the further functionalities of the Quanlse Trapped Ion module."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
    "\\[1\\] [K. Kim, et al. \"Entanglement and Tunable Spin-Spin Couplings Between Trapped Ions Using Multiple Transverse Modes.\" *Physical Review Letters* 105, 109901 (2010)](https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.103.120502)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
